import wsgiref.handlers
from google.appengine.ext import webapp

import Applicant

class ApplyDraw( webapp.RequestHandler ):

    def get( self ):
        self.response.headers['Content-Type'] = 'text/html'

        self.response.out.write(
'''
<form action="" method="post" accept-charset="utf-8">
  <fieldset>
    <legend>Enter Lucky Draw</legend>
    <table>
    <tr><td>Tell me your Name:</td><td><input type="text" name="name" id="name" /></td></tr>
    <tr><td colspan="2"><button type="submit">Enter!</button></td></tr>
    </table>
  </fieldset>
</form>
<a href="http://appengine.google.com/" target="_blank"><img
src="http://code.google.com/appengine/images/appengine-silver-120x30.gif"
alt="Powered by Google App Engine" style="border:none;" /></a>
''' )
    # end of ApplyDraw.get

    def post( self ):
        self.response.headers['Content-Type'] = 'text/plain'

        name = self.request.get( 'name' )

        r = Applicant.Applicant \
          .gql( 'WHERE name = :1', name ) \
          .get()

        if r is not None:
            self.response.out.write(
                'Hi %s ~ You have already entered the draw =]' % name
            )
            return

        if name is '':
            self.response.out.write( 'Tell me your name please =]' )
            return

        record = Applicant.Applicant()
        record.name = name

        record.put()

        self.response.out.write(
            'Hi %s ~ You just entered the draw =]' % name
        )
    # end of ApplyDraw.post

# end of class ApplyDraw
